/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        
        if(nullptr==head || nullptr==head->next){
            return head;
        }
        ListNode dummy;
        ListNode* pre=&dummy;
        pre->next=head;
        ListNode* next=head->next;
        ListNode* cur=head;
        while(cur && next){
            ListNode* nextCur=next->next;
            pre->next=next;
            next->next=cur;
            cur->next=nextCur;
            pre=cur;
            cur=nextCur;
            next=nullptr;
            if(cur){
                next=cur->next;
            }
        }

        return dummy.next;
    }
};
